/**
 * Copyright 2018-2020 stylefeng & lwj (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mbl.base.core.common.page;

import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbl.base.core.utils.UtilHttp;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * Layui Table默认的分页参数创建
 *
 * @author lwj
 * @date 2017-04-05 22:25
 */
public class LayuiPageFactory {

    /**
     * 获取layui table的分页参数
     *
     * @author lwj
     * @Date 2019/1/25 22:13
     */
    public static Page defaultPage() {
        HttpServletRequest request = UtilHttp.getRequest();
        //每页多少条数据
        String limit1 = request.getParameter("limit");
        if(StringUtils.isEmpty(limit1)){
            limit1 = "15";
        }
        int limit = Integer.valueOf(limit1);
        //第几页
        String page1 = request.getParameter("page");
        if(StringUtils.isEmpty(page1)){
            page1 = "1";
        }
        final Page page = new Page(Integer.valueOf(page1), limit);
        final String order = request.getParameter("order");
        final String sort = request.getParameter("sort");
        if(StringUtils.isNoneBlank(order, sort)){
            if(order.equals("desc")){
                page.setDesc(sort);
            } else {
                page.setAsc(sort);
            }
        }
        return page;
    }

}
